package org.ben.birthday.twentybirthday.service.impl;

import org.ben.birthday.twentybirthday.dao.UserDaoRepository;
import org.ben.birthday.twentybirthday.entity.User;
import org.ben.birthday.twentybirthday.service.IAdministratorService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.Iterator;
import java.util.List;

/**
 * @Author: Ben
 * @Date: 2019/7/25 8:03
 */
@Service
public class AdministratorImpl implements IAdministratorService {

    @Autowired
    UserDaoRepository userDaoRepository;

    private static final Logger logger = LoggerFactory.getLogger(AdministratorImpl.class);

    /**
     * 管理员添加用户ServiceImpl
     * @param user
     * @return boolean
     */
    @Override
    public boolean insertUserService(User user) {
        List<User> userList=userDaoRepository.findUsersByUserName(user.getUserName());
        Iterator<User> userIterator=userList.iterator();
        while (userIterator.hasNext()){
            logger.error("用户名已存在，添加失败");
            return false;
        }
        userDaoRepository.save(user);
        logger.info("添加成功");
        return true;
    }

    /**
     * 管理员删除用户ServiceImpl
     * @param userId
     * @param userName
     * @return boolean
     */
    @Override
    public boolean deleteUserService(Integer userId,String userName) {
        List<User> userList = userDaoRepository.findUsersByUserName(userName);
        Iterator<User> userIterator = userList.iterator();
        while (userIterator.hasNext()) {
            userDaoRepository.deleteById(userId);
            logger.info("用户删除成功");
            return true;
        }
        logger.error("该用户不存在，无法删除");
        return false;
    }

    /**
     * 管理员更改用户信息ServiceImpl
     * @param user
     * @return boolean
     */
    @Override
    public boolean updateUserService(User user) {
     List<User> userList=userDaoRepository.findUsersByUserName(user.getUserName());
      Iterator<User> userIterator=userList.iterator();
      while(userIterator.hasNext()){
          userDaoRepository.saveAndFlush(user);
          logger.info("更改成功");
          return true;
      }
      logger.error("查无此用户，更改失败");
        return false;
    }

    /**
     * 管理员分页查询用户数据ServiceImpl
     * @param pageNumber
     * @param pageSize
     * @return Page<User>
     */
    @Override
    public Page<User> selectUser(Integer pageNumber, Integer pageSize) {
        PageRequest pageable = PageRequest.of (pageNumber, pageSize);
        Page<User> pageUser = userDaoRepository.findAll(pageable);
        logger.info("总记录数: " + pageUser.getTotalElements());
        logger.info("当前第几页: " + (pageUser.getNumber() + 1));
        logger.info("总页数: " + pageUser.getTotalPages());
        logger.info("当前页面的 List: " + pageUser.getContent());
        logger.info("当前页面的记录数: " + pageUser.getNumberOfElements());
        return pageUser;
    }
}
